const app = getApp();
var api = require('../../../api.js');

Page({
  data: {
    list:[],
    zero: "您还没有提现记录，请加油！",
    judge: 0,
    pageCount: 1, //加载更多的分页数
    allPage: 0,  //总分页
  },
  onLoad: function (options) {

    app.bind();
    this.getWithdraw();
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {

    // app.bind();
    // this.getWithdraw();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();

  },
  onReachBottom: function () {

    var that = this;
    // 分页数
    var allPage = that.data.allPage;
    var pageCount = that.data.pageCount;
    pageCount ++;
    that.setData({
      pageCount: pageCount,
    })

    if (pageCount <= allPage) {
      that.getWithdraw();
    } else {
      wx.showToast({
        title: '全部加载完成，不能加载更多',
        icon: 'none',
      })
    }

  },
  onShareAppMessage: function () {
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "?user_id=" + user_id,
      title: '得道1314'
    };  
  },
  // 开始时信息获取
  getWithdraw: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token);
    var url = api.user.cashDraw;
    var pageCount = that.data.pageCount;
    url = url + "&page=" + pageCount;

    wx.request({
      url: url,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e);
        var allPage = e.data.data.page_count;
        var list = e.data.data.list;
        var listOrigin = that.data.list;
        var listLast = listOrigin.concat(list);

        if (e.data.code == 0) {

          if (e.data.data.row_count == 0) {
            that.setData({
              judge: 1,
            }) 
          }

          that.setData({
            allPage: allPage,
            list: listLast,
          })
          
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
  // 加载更多
  getMore: function (e) {
    var that = this;
    var pageCount = that.data.pageCount;
    pageCount ++;
    that.setData({
      pageCount: pageCount,
    })
    var access_token = wx.getStorageSync('access_token');
    var url = api.user.cashDraw;
    url = url + "&page=" + pageCount;
    console.log(url);
    wx.request({
      url: url,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
      },
      success: function (e) {
        console.log(e);
        // var list = e.data.data.list;
        // console.log(list);
        // if (e.data.code == 0) {

        //   if (e.data.data.row_count == 0) {
        //     that.setData({
        //       judge: 1,
        //     }) 
        //   }

        //   that.setData({
        //     list: e.data.data.list
        //   })
          
        // } else {
        //   wx.navigateTo({
        //     url: '/pages/grant/grant'
        //   })
        // }
      }
    });
  }

})